package com.powergisol.gis.service.impl;

import com.powergisol.gis.dao.TabCoordinateMapper;
import com.powergisol.gis.entity.TabCoordinate;
import com.powergisol.gis.service.TabCoordinateService;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.TabCoordinateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Date: 2019/9/17  10:12
 * Description:
 */
@Service
public class TabCoordinateServiceImpl implements TabCoordinateService {
    @Autowired
    private TabCoordinateMapper tabCoordinateMapper;

    @Override
    public List<TabCoordinateVo> tabCoordinateList(TabCoordinateVo vo) {
        return tabCoordinateMapper.tabCoordinateList(vo);
    }

    @Override
    public Result saveCoordinate(TabCoordinateVo vo) {
        Result result = new Result();
        if(null == vo.getEpsg()||null == vo.getName()||"".equals(vo.getEpsg())||"".equals(vo.getName())){
            return result.failure(-1,"坐标系编码或名称不能为空");
        }
        Integer countEpsg = tabCoordinateMapper.countEpsg(vo.getEpsg());
        if(countEpsg>0){
            return result.failure(-1,"坐标系编码不能重复");
        }
        TabCoordinate tabCoordinate = new TabCoordinate();
        BeanUtils.copyProperties(vo,tabCoordinate);
        tabCoordinateMapper.insertSelective(tabCoordinate);
        return result.success("新增成功");
    }

    @Override
    public Result updateCoordinate(TabCoordinateVo vo) {
        Result result = new Result();
        if(null == vo.getEpsg()||null == vo.getName()||"".equals(vo.getEpsg())||"".equals(vo.getName())){
            return result.failure(-1,"坐标系编码或名称不能为空");
        }
        TabCoordinate tabCoordinate = tabCoordinateMapper.selectByPrimaryKey(vo.getId());
        Integer countEpsg = tabCoordinateMapper.countEpsg(vo.getEpsg());
        if(countEpsg>0 && !tabCoordinate.getEpsg().equals(vo.getEpsg())){
            return result.failure(-1,"坐标系编码不能重复");
        }

        BeanUtils.copyProperties(vo,tabCoordinate);
        tabCoordinateMapper.updateByPrimaryKeySelective(tabCoordinate);
        return result.success("修改成功");
    }

    @Override
    public void delCoordinate(Integer id) {
        tabCoordinateMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>> selectTabCoordinate() {
        return tabCoordinateMapper.selectTabCoordinate();
    }

    @Override
    public Map<String, Object> selectProByEspg(String epsg) {
        return tabCoordinateMapper.selectProByEspg(epsg);
    }
}
